package com.common.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
 * @project: ssm_sms
 * @description: 绘制验证码图片
 */

public class VerifyCodeUtil {
        private static int WIDTH = 90;
        private static int HEIGHT = 35;
        private static int FONT_SIZE = 20; //字符大小
        private static char[] verifiCode; //验证码
        private static BufferedImage verifiCodeImage; //验证码图片

        /**
         * @description: 获取验证码图片
         * @param: no
         * @return: java.awt.image.BufferedImage
         */
        public static BufferedImage getVerifiCodeImage() {

            //通过buffer，生成一个空白验证码图片
            verifiCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);// create a image

            //verifiCodeImage对象的Graphics对象，并赋值给graphics变量。Graphics对象是一个Java类，用于在图像上绘制图形和文本。getGraphics()方法是BufferedImage类的一个方法，
            // 返回一个可以修改图像的Graphics对象。通过使用graphics变量，你可以设置要在verifiCodeImage对象上绘制的文本的字体、颜色和位置。
            Graphics graphics = verifiCodeImage.getGraphics();

            //调用内部方法生成验证码
            verifiCode = generateCheckCode();

            //调用内部方法，用于绘制验证码图片的背景颜色和干扰点。
            drawBackground(graphics);
            //调用内部方法，用于绘制验证码内容到图片上。
            drawRands(graphics, verifiCode);

            //释放graphics对象占用的资源。
            graphics.dispose();

            return verifiCodeImage;
        }

        /**
         * @description: 获取验证码
         * @param: no
         * @return: char[]
         */
        public static char[] getVerifiCode() {
            return verifiCode;
        }

        /**
         * @description: 随机生成验证码
         * @param: no
         * @return: char[]
         */
        private static char[] generateCheckCode() {
            String chars = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char[] rands = new char[4];
            for (int i = 0; i < 4; i++) {
                int rand = (int) (Math.random() * (10 + 26 * 2));
                rands[i] = chars.charAt(rand);
            }
            return rands;
        }

        /**
         * @description: 绘制验证码
         * @param: g
         * @param: rands
         * @return: void
         */
        private static void drawRands(Graphics g, char[] rands) {
            g.setFont(new Font("Console", Font.BOLD, FONT_SIZE));

            for (int i = 0; i < rands.length; i++) {

                g.setColor(getRandomColor());
                g.drawString("" + rands[i], i * FONT_SIZE + 10, 25);
            }
        }

        /**
         * @description: 绘制验证码图片背景
         * @param: g
         * @return: void
         */
        private static void drawBackground(Graphics g) {

            g.setColor(Color.white);
            g.fillRect(0, 0, WIDTH, HEIGHT);

            // 绘制验证码干扰点
            for (int i = 0; i < 200; i++) {
                int x = (int) (Math.random() * WIDTH);
                int y = (int) (Math.random() * HEIGHT);
                g.setColor(getRandomColor());
                g.drawOval(x, y, 1, 1);

            }
        }

        /**
         * @description: 获取随机颜色
         * @param: no
         * @return: java.awt.Color
         */
        private static Color getRandomColor() {
            Random ran = new Random();
            return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
        }
}
